﻿using Abp.Domain.Entities;
using Abp.Domain.Entities.Auditing;
using Microsoft.EntityFrameworkCore;
using shenghua.Common.CustomValueType;
using shenghua.Order.Orders.Median;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace shenghua.Marketing.Coupons
{
    /// <summary>
    /// 优惠券
    /// </summary>
    public class Coupon : Entity, ICreationAudited
    {
        public string Name { get; set; }

        public Money Amount { get; set; }



        public CouponCondition Condition { get; set; }

        public TimeLimit TimeLimit { get; set; }

        public CouponTargetType TargetType { get; set; }

        /// <summary>
        /// 指定商品
        /// </summary>
        public ValueArray<int> AppliedCommodityIds { get; set; }

        /// <summary>
        /// 优惠券总数
        /// </summary>
        public int TotalCount
        {
            get
            {
                if (IsInfinite) return int.MaxValue;
                else return RestCount + GrantedCount;
            }
        }

        /// <summary>
        /// 无限券数
        /// </summary>
        public bool IsInfinite { get; set; }

        /// <summary>
        /// 优惠券已发放数
        /// </summary>
        public int GrantedCount { get; set; }

        /// <summary>
        /// 优惠券使用数
        /// </summary>
        public int UsedCount { get; set; }

        /// <summary>
        /// 优惠券剩余数
        /// </summary>
        public int RestCount { get; set; }

        public bool HasRest
        {
            get
            {
                if (IsInfinite) return true;
                else return RestCount > 0;
            }
        }

        public Coupon()
        {
        }

        public bool IsContainCommodity(int commodityId)
        {
            if (TargetType == CouponTargetType.All)
            {
                return true;
            }
            else
            {
                return AppliedCommodityIds.Items.Contains(commodityId);
            }
        }

        public bool CanUse(List<CommodityCollectionItem> items)
        {
            if (!Enabled) { return false; }

            Money totalAmount = new Money();
            int commodityCount = 0;

            foreach(var item in items)
            {
                if (item.HasSalesPromotion) continue;
                if (!IsContainCommodity(item.Commodity.CommodityId)) continue;
                commodityCount += item.Count;
                totalAmount += item.TotalAmount;
            }

            if(totalAmount.Value > Condition.MinAmount.Value &&
                commodityCount >= Condition.MinCount)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 优惠券是否有效
        /// </summary>
        public bool Enabled
        {
            get
            {
                DateTime now = DateTime.Now;
                return now >= TimeLimit.Begin && now <= TimeLimit.End;
            }
        }

        public long? CreatorUserId { get; set; }
        public DateTime CreationTime { get; set; }
    }

    [Owned]
    public class CouponCondition
    {
        public Money MinAmount { get; set; }
        public int MinCount { get; set; }

        public CouponCondition()
        {
            MinAmount = new Money(0);
            MinCount = 0;
        }
    }
    
    public enum CouponTargetType
    {
        /// <summary>
        /// 部分商品
        /// </summary>
        All = 0,

        /// <summary>
        /// 全部商品
        /// </summary>
        Part = 1
    }
}
